<?php 
	function subtotal()
	{
		$subtotal=0;
		if(isset($_SESSION["giohang"]))
		{
			foreach($_SESSION["giohang"] as $k=>$gt)
			{
					$qty=$gt[1];
					$price=$gt[2];
					$subtotal+= $qty * $price;
			}
		}
		return format($subtotal);
	}
	function list_giohang()
	{
		$list="";
		if(isset($_SESSION["giohang"]))
		{
			foreach($_SESSION["giohang"] as $k=>$gt)
			{
					$name=chooseName($gt[0]);
					$qty=$gt[1];
					$price=$gt[2];
					$sum=$price*$qty;
					$list=$qty.' x '.$name.' @ '.$price.' $ch = '.format($sum).' $<br/>';
			}
		}
		return $list;
	}
	function sumTotal()
	{
		if(isset($_SESSION["giohang"])){
		$sum=subtotal()+25.00+29.00+5.00;}
		else $sum=0;
		return format($sum);
	}
	function check_number($number)
	{
		if(!is_numeric($number) || $number <1 || $number >100)
			return false;
		return true;
	}
	
	function set_qty($loai)
	{
		if(isset($_SESSION['giohang'][$loai])) 
		{
			echo $_SESSION['giohang'][$loai][1];
		}
		else
			echo "1";
	}
	function set_price($loai,$price)
	{
		if(isset($_SESSION['giohang'][$loai])) 
		{
			$total_price=$price*$_SESSION['giohang'][$loai][1]; 
			echo format($total_price);
			
		}
		else echo $price;
	}
	function getValue($loai,$number)
	{
		$gt="";
		if(isset($_SESSION['giohang'][$loai]))
			$gt=$_SESSION['giohang'][$loai][$number];
		return $gt;
	}
	function checkExists($loai)
	{
		if(isset($_SESSION['giohang'][$loai]))
			return true;
		return false;
	}
	function chooseName($loai)
	{
		switch($loai)
		{
			case "planter":
				$name="Arbres";break;
			case "don":
				$name="Don unique";break;
			case "trousse1":
				$name="Trousse d'économie d'eau #1 - Classique";break;
			case "trousse2":
				$name="Trousse d'économie d'eau #2 - Deluxe";break;
			case "trousse3":
				$name="Trousse d'économie d'eau #3 - l'Essentiel";break;
			default : 
				$name="Trousse d'économie d'eau #4 - l'Extérieur";break;
		}
		return $name;
	}
	function format($gt)
	{
		return number_format($gt, 2, '.', '');
	}
	function geterror($number)
	{
		if(isset($number)){
			switch($number)
			{
				case 1:
					$error="This value should be a valid number.";
					break;
				default :
					$error="This value is required.";
					break;
			}
			echo '<p class="empty mess-error">'.$error.'</p>';
		}
	}
	function getparam($p=NULL)
	{
		$url=(!isset($_GET["md"]))?"":$_GET["md"];
		switch($url)
		{
			case "cart":
				$title="Cart";
				$page="cart.php";
				break;
			case "trousse":
				$title="Acheter une trousse d’économie d’eau";
				$page="trousse.php";
				break;
			case "don":
				$title="Don d’argent en ligne";
				$page="don.php";
				break;
			case "payment":
				$title="Paiement";
				$page="payment.php";
				break;
			case "confirm":
				$title="Confirm";
				$page="confirm.php";
				break;
			default:
				$title="Planter des arbres";
				$page="home.php";
				break;
		}
		if($p=="title")	return $title;
		else return $page;
	}
	function chooseCard($name)
	{
		switch($name)
		{
			case "M";
				$cart="Mastercard";break;
			case "AX":
				$cart="American Express ";break;
			case "DC":
				$cart="Diners Card";break;
			case "NO":
				$cart="Novus / Discover ";break;
			case "SE ":
				$cart="Sears";break;
			default:
				$cart="Visa";break;
		}
		return $cart;
	}

?>